package com.example.app01

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils.substring
import android.util.Log
import android.view.View
import android.widget.TextView
import com.example.app01.service.MyClass
import com.example.app01.service.Student
import com.example.app01.service.StudentService
import com.google.gson.Gson
import okhttp3.ResponseBody
import okio.ByteString.Companion.toByteString
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


class MainActivity : AppCompatActivity() {
    val retrofit = Retrofit.Builder()
        .baseUrl("http://10.0.2.2:8080/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //4、使用retrofit返回数据解析json格式:打印json数据
        val myClass = retrofit.create(MyClass::class.java)
        myClass.getStudentSignMessage("1001").enqueue(object : Callback<ResponseBody>{
            override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                var result = ""
                response.body()?.let { result = it.string() }
                Log.e("result", result)
            }
            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                Log.d("请求数据失败","error")
            }

        })


        //总结：使用retrofit，可以实现数据提交和返回数据转化
        //3、使retrofit解析数据返回数据直接构造成一个类

//        val studentService = retrofit.create(StudentService::class.java)
//        studentService.getStudentData("张三").enqueue(object: Callback<List<Student>>{
//            override fun onResponse(call: Call<List<Student>>, response: Response<List<Student>>) {
//                val textView = findViewById<View>(R.id.textView) as TextView
//                val list = response.body()
//                Log.d("responseMessage",response.toString())
//                if(list!=null){
//                    for (student in list){
//                        Log.d("student",student.toString())
//                        textView.text = student.toString()
//                    }
//                }
//            }
//            override fun onFailure(call: Call<List<Student>>, t: Throwable) {
//                Log.d("error","error")
//            }
//        })


        //2、使用okhttp封装好的回调方法:测试成功
//        HttpUtil.sendOkHttpRequest("http://10.0.2.2:8080/student/queryAllStudent",object : Callback {
//            override fun onFailure(call: Call, e: IOException) {
//                Log.d("exception",e.printStackTrace().toString());
//            }
//
//            override fun onResponse(call: Call, response: Response) {
//                Log.d("Response",response.toString());
//            }
//        })

        //1、测试失败：在线程中不能返回数据
//        val myOkHttp =  MyOkHttp("https://www.baidu.com/")
//        myOkHttp.sendRequestByGet()
//        sleep(1000);
//        val responseData = myOkHttp.getResponseData()
////        Log.d("response","response")
//        if (responseData != null) {
//            Log.d("response","responseData")
//            Log.d("response",responseData)
////            showResponse(responseData)
//        }else{
//            Log.d("response","error")
//
//        }
    }
    private fun showResponse(response: String?){
        runOnUiThread{
            val textView = findViewById<View>(R.id.textView) as TextView
            textView.text = response;

        }
    }
}